//
//  CLRMFontSizeView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/8/4.
//

#import "CLRMFontSizeView.h"
#import "CLReadMenu.h"
@interface CLRMFontSizeView()
@property (nonatomic, strong) UILabel *title;

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UILabel *fontSize;

@property (nonatomic, strong) UIButton *displayProgress;
@end

@implementation CLRMFontSizeView

- (void)addSubviews {
    [super addSubviews];
    
    self.backgroundColor = UIColor.clearColor;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton = leftButton;
    leftButton.titleLabel.font = CL_FONT_SA_12;
    [leftButton setTitle:@"A-" forState:(UIControlStateNormal)];
    [leftButton setTitleColor:CL_READ_COLOR_MENU_COLOR forState:(UIControlStateNormal)];
    leftButton.layer.cornerRadius = CL_SPACE_SA_6;
    leftButton.layer.borderColor = CL_READ_COLOR_MENU_COLOR.CGColor;
    leftButton.layer.borderWidth = CL_SPACE_1;
    [leftButton addTarget:self action:@selector(clickLeftButton) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton = rightButton;
    rightButton.titleLabel.font = CL_FONT_SA_14;
    [rightButton setTitle:@"A+" forState:(UIControlStateNormal)];
    [rightButton setTitleColor:CL_READ_COLOR_MENU_COLOR forState:(UIControlStateNormal)];
    rightButton.layer.cornerRadius = CL_SPACE_SA_6;
    rightButton.layer.borderColor = CL_READ_COLOR_MENU_COLOR.CGColor;
    rightButton.layer.borderWidth = CL_SPACE_1;
    [rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:rightButton];
    

    UILabel *fontSize = [[UILabel alloc]init];
    self.fontSize = fontSize;
    fontSize.text = @(CLReadConfigure.sharedInstance.fontSize).stringValue;
    fontSize.font = CL_FONT_SA_16;
    fontSize.textColor = CL_READ_COLOR_MENU_COLOR;
    fontSize.textAlignment = NSTextAlignmentCenter;
    [self addSubview:fontSize];
    
    UIButton *displayProgress = [UIButton buttonWithType:UIButtonTypeCustom];
    self.displayProgress = displayProgress;
    [displayProgress setBackgroundImage:[[UIImage imageNamed:@"page"]imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)] forState:(UIControlStateNormal)];
    [displayProgress addTarget:self action:@selector(clickDisplayProgress:) forControlEvents:(UIControlEventTouchUpInside)];
    displayProgress.selected = @(CLReadConfigure.sharedInstance.progressIndex).boolValue;
    [self addSubview:displayProgress];
    [self updateDisplayProgressButton];
}

- (void)clickDisplayProgress:(UIButton *)button {
    
    button.selected = !button.isSelected;
    
    [self updateDisplayProgressButton];
    
    CLReadConfigure.sharedInstance.progressIndex = button.isSelected;
    
    [CLReadConfigure.sharedInstance save];
    
    if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickDisplayProgress:)]) {
        [self.readMenu.delegate readMenuClickDisplayProgress:self.readMenu];
    }
}

/// 刷新日夜间按钮显示状态
- (void)updateDisplayProgressButton {
    if (self.displayProgress.isSelected) {
        self.displayProgress.tintColor = CL_READ_COLOR_MAIN;
    } else {
        self.displayProgress.tintColor = CL_READ_COLOR_MENU_COLOR;
    }
}

- (void)clickLeftButton {
    
    NSNumber *size = @(CLReadConfigure.sharedInstance.fontSize - CL_READ_FONT_SIZE_SPACE);
    
    if (!(size.intValue < CL_READ_FONT_SIZE_MIN)) {
        
        self.fontSize.text = size.stringValue;
        
        CLReadConfigure.sharedInstance.fontSize = size.integerValue;
        
        [CLReadConfigure.sharedInstance save];
        
        if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickFontSize:)]) {
            [self.readMenu.delegate readMenuClickFontSize:self.readMenu];
        }
    }
}

- (void)clickRightButton {
    
    NSNumber *size = @(CLReadConfigure.sharedInstance.fontSize + CL_READ_FONT_SIZE_SPACE);
    
    if (!(size.intValue > CL_READ_FONT_SIZE_MAX)) {
        
        self.fontSize.text = size.stringValue;
        
        CLReadConfigure.sharedInstance.fontSize = size.integerValue;
        
        [CLReadConfigure.sharedInstance save];
        
        if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickFontSize:)]) {
            [self.readMenu.delegate readMenuClickFontSize:self.readMenu];
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    
    CGFloat itemW = CL_SPACE_SA_100;
    CGFloat itemH = CL_SPACE_SA_30;
    CGFloat itemY = (h - itemH) / 2;
    CGFloat displayProgressWH = CL_SPACE_SA_45;
    
    self.leftButton.frame = CGRectMake(0, itemY, itemW, itemH);
    
    self.rightButton.frame = CGRectMake(w - itemW - displayProgressWH - CL_SPACE_SA_25, itemY, itemW, itemH);
    
    self.fontSize.frame = CGRectMake(CGRectGetMaxX(self.leftButton.frame), itemY, CGRectGetMinX(self.rightButton.frame) - CGRectGetMinX(self.leftButton.frame), itemH);
    
    self.displayProgress.frame = CGRectMake(w - displayProgressWH - CL_SPACE_SA_2, (h - displayProgressWH) / 2, displayProgressWH, displayProgressWH);
}

@end
